from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
import pandas as pd
from application.utils.general_tool import gcfnpdap
import time
import random


def xlsx_excel_to_vector(text_knowledge_file_name: str):
    """
    desc: 将xlsx文件数据转换为可使用的向量数据，xlsx必须包含input和instruction列，并返回新建faiss文件夹名
    text_knowledge_file_name: 文本知识库文件名
    :return: 新建faiss文件夹名
    """
    embeddings = OpenAIEmbeddings(model="text-embedding-3-large")

    # get text knowledge dic absolute path
    store_text_dir = gcfnpdap(__name__, 3) + "/static/store_text_knowledge_dictory/" + "小何个人知识库.xlsx"

    df = pd.read_excel(store_text_dir)
    instruction_column = df["instruction"].tolist()
    input_column = df["input"].tolist()
    # create a list to store embedding vector
    vector_embedding = list()

    for i in range(len(instruction_column)):
        vector_embedding.extend(embeddings.embed_documents(instruction_column[i]))
    text_vector_pairs = list(zip(input_column, vector_embedding))
    excel_text_to_vector = FAISS.from_embeddings(text_vector_pairs, embeddings)

    # get store vector's absolute path
    store_vector_dir = gcfnpdap(__name__, 3) + "/static/store_vector_knowledge_dictory/"

    faiss_dir_name = str(int(time.time())) + "_" + str(random.randint(0, 10))

    # faiss file dir name
    dir_absolute_path = store_vector_dir + faiss_dir_name

    excel_text_to_vector.save_local(folder_path=dir_absolute_path)

    return faiss_dir_name


